#/usr/bin/python
# coding: utf8

import httplib, urllib, sys, re, base64

def lgRouter():
    # 192.168.1.1
    conn = httplib.HTTPConnection("192.168.1.1")
    name = 'admin'
    passwd = 'admin'
    # set request headers
    headers = {"User-Agent": "python host",
        "Content-type": "application/x-www-form-urlencoded",
        "Authorization": "Basic %s" % base64.encodestring('%s:%s' % (name, passwd))[:-1],
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Language": "zh-cn,zh;q=0.5",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Charset": "GB2312,utf-8;q=0.7,*;q=0.7",
        "Connection": "keep-alive"}        

    # get status page
    conn.request("GET", "/userRpm/StatusRpm.htm", "", headers)
    response = conn.getresponse()
    keyword = re.search(' wanPara [^\)]*?\)', response.read())
    response.close()
    conn.close()

    # search the public ip address
    found = 0
    publicIP = ""
    if keyword:
        arr = re.findall('([\d]*?,)|(\"[^\"]*?\",)', keyword.group(0))
        if arr:
            if len(arr) > 3:
                publicIP = re.search('(?<=\")[^\"]*?(?=\")', arr[2][1])
                if publicIP:
                    publicIP = publicIP.group(0)
                    found = 1

    if found == 1:
       print publicIP
    else:
        if showErrorMessage == 1:
            print "public ip address not found."
            
            
lgRouter()
